//
//  DMRegistCController.m
//  DMGME
//
//  Created by ihope99 on 15/9/8.
//  Copyright (c) 2015年 GYP. All rights reserved.
//

#import "DMRegistCController.h"
#import "ProvinceController.h"
#import "ExamineController.h"
#import "DMStoreInfoController.h"
#import "DMRegistDController.h"


@interface DMRegistCController ()

@end

@implementation DMRegistCController

- (void)viewDidLoad {
    [super viewDidLoad];
    _mDicID = [NSMutableDictionary dictionary];
    // Do any addi
    [self setTitle:@"注册1"];
    self.view.backgroundColor = [UIColor whiteColor];
    [self AddLeftImageBtn:[UIImage imageNamed:@"fanhui_03 2"]target:self action:@selector(GoBack)];
    
    _provinceSetBtn.tag = 2000;
    _citySetBtn.tag = 2001;
    _countySetBtn.tag = 2002;
    
    //选择省
    [_provinceSetBtn addTarget:self action:@selector(_provinceSetBtnClike:) forControlEvents:UIControlEventTouchUpInside];
   
    //选择市
    [_citySetBtn addTarget:self action:@selector(_provinceSetBtnClike:) forControlEvents:UIControlEventTouchUpInside];
    
    //选择区/县
    [_countySetBtn addTarget:self action:@selector(_provinceSetBtnClike:) forControlEvents:UIControlEventTouchUpInside];
    
    //选择上级代理
    [_superiorAgenBtn addTarget:self action:@selector(_superiorAgenBtnClike:) forControlEvents:UIControlEventTouchUpInside];
    
    //店铺信息
    [_storeInfoBtn addTarget:self action:@selector(_storeInfoBtnClike:) forControlEvents:UIControlEventTouchUpInside];

    
    //申请验证
    [_applyVerificationBtn addTarget:self action:@selector(_applyVerificationBtnClike:) forControlEvents:UIControlEventTouchUpInside];
    
    if ([_reChooseSign isEqual:@"re"]) {
        
        [_storeInfoBtn setTitle:@"您已创建" forState:UIControlStateNormal];
        _storeInfoBtn.enabled = NO;

    }
    
}
- (void)_storeInfoBtnClike:(UIButton *)btn
{
    //phoneNumstr
    DMStoreInfoController *dmstro = [[DMStoreInfoController alloc] initWithNibName:@"DMStoreInfoController" bundle:nil];
    dmstro.mNamestr = _mNamestr;
    dmstro.mPhoneNumstr = _phoneNumstr;

    [dmstro ShopId:^(NSString *shopId) {
        [_storeInfoBtn setTitle:@"您已创建" forState:UIControlStateNormal];
        _shopId = shopId;
}];
    [self.navigationController pushViewController:dmstro animated:YES];
}

- (void)ShowAlert:(NSString *)text {
    UILabel *msg = [[UILabel alloc] initWithFrame:CGRectMake((self.view.bounds.size.width-250)/2, (self.view.bounds.size.height-80)/2, 250, 80)];
    msg.text = text;
    msg.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.7];
    msg.textColor = [UIColor whiteColor];
    msg.font = [UIFont systemFontOfSize:14];
    msg.numberOfLines = 0;
    msg.textAlignment = NSTextAlignmentCenter;
    msg.layer.cornerRadius = 10;
    msg.clipsToBounds = YES;
    
    [self.view addSubview:msg];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [msg removeFromSuperview];
   [self.navigationController popToRootViewControllerAnimated:YES];
    });
}
- (void)_applyVerificationBtnClike:(UIButton *)btn
{
    if ([_provinceSetBtn.titleLabel.text isEqualToString:@"点击设置"]) {
        [self showMsg:@"请选择您的省位置"];
    }else if ([_citySetBtn.titleLabel.text isEqualToString:@"点击设置"]){
        [self showMsg:@"请选择您的市位置"];
    }else if ([_superiorAgenBtn.titleLabel.text isEqualToString:@"点击选择"]){
        [self showMsg:@"请选择您上级代理商"];
    }else if ([_storeInfoBtn.titleLabel.text isEqualToString:@"点击设置"]){
        [self showMsg:@"请设置您的店铺信息"];
    }
    else{
    if ([_reChooseSign isEqual:@"re"]) {
//        NSLog(@"11111%@",kkUserDict);
        
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        
        [dict setValue: _mDicID[@"省"] forKey:@"province_id"];
        [dict setValue: _mDicID[@"市"] forKey:@"city_id"];
        [dict setValue: _mDicID[@"县/区"] forKey:@"country_id"];
        [dict setValue: _pID forKey:@"pid"];
        [dict setValue: kkUserDict[@"id"] forKey:@"id"];
        
        [dict setValue: kkUserDict[@"id"] forKey:@"shop_id"];
        
        [dict setValue: [AFDownManager MD5String:@"%^&*dongmangoug!@#$registerAgain"] forKey:@"token"];
        [dict setValue: @"registerAgain" forKey:@"action"];
        
//       // NSLog(@"%@",dict);
        [AFDownManager POST:REGISTERNET parameters:dict success:^(id responseObject) {
//            NSLog(@"%@",responseObject);
//            NSLog(@"%@",responseObject[@"desc"]);
            if ([responseObject[@"code"] intValue] == 0) {
                
                [self ShowAlert:@"操作已成功！请耐心等候审核结果"];
               
            }else{
                [self showMsg:responseObject[@"desc"]];

            }
            
        } failure:^(NSError *error) {
        }];
        
    }else{
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    [dict setValue: _mDicID[@"省"] forKey:@"province_id"];
    [dict setValue: _mDicID[@"市"] forKey:@"city_id"];
    [dict setValue: _mDicID[@"县/区"] forKey:@"country_id"];
    [dict setValue: _pID forKey:@"pid"];
    [dict setValue: [NSString stringWithFormat:@"%@",_shopId] forKey:@"shop_id"];
    [dict setValue: _phoneNumstr forKey:@"mobile"];
    [dict setValue: _mNamestr forKey:@"realname"];
    [dict setValue: _codeID forKey:@"captcha"];
//        NSLog(@"%@",dict);

    DMRegistDController *dmregist = [[DMRegistDController alloc] initWithNibName:@"DMRegistDController" bundle:nil];
    dmregist.titleType = @"1";
    dmregist.mDict = dict;
    dmregist.titleStr = [NSString stringWithFormat:@"您的身份已转至:\n%@审核\n请输入密码完成注册",_shopManastr];
    [self.navigationController pushViewController:dmregist animated:YES];
     }
    }
    
}
- (void)_provinceSetBtnClike:(UIButton *)sender{
    ProvinceController *provinVC = [[ProvinceController alloc] init];
    if (sender.tag == 2000) {
        provinVC.provinceType = @"省";
        [_citySetBtn setTitle:@"点击设置" forState:UIControlStateNormal];
        [_countySetBtn setTitle:@"点击设置" forState:UIControlStateNormal];
        [self.navigationController pushViewController:provinVC animated:YES];

        
    }else if (sender.tag == 2001){
        
        if ([_provinceSetBtn.titleLabel.text isEqualToString:@"点击设置"]) {
            [self showMsg:@"请先选择您的省位置"];
            return;
        }
        provinVC.provinceType = @"市";
        provinVC.provinceTypeId = _provinceId;
        [_countySetBtn setTitle:@"点击设置" forState:UIControlStateNormal];
        [self.navigationController pushViewController:provinVC animated:YES];


    }else if (sender.tag == 2002){
        
        if ([_provinceSetBtn.titleLabel.text isEqualToString:@"点击设置"]) {
            [self showMsg:@"请先选择您的省位置"];
            return;
        }else if ([_citySetBtn.titleLabel.text isEqualToString:@"点击设置"]){
            [self showMsg:@"请先选择您的市位置"];
            return;
        }
        provinVC.provinceType = @"县/区";
        provinVC.provinceTypeId = _cityId;
        [self.navigationController pushViewController:provinVC animated:YES];

    }

    [provinVC provinceStr:^(NSString *provinceStr, NSString *provinceId) {
        [sender setTitle:provinceStr forState:UIControlStateNormal];
        if (sender.tag == 2000) {
            _provinceId = provinceId;
            [_mDicID setValue:provinceId forKey:@"省"];
        }else if (sender.tag == 2001){
            _cityId = provinceId;
            [_mDicID setValue:provinceId forKey:@"市"];
        }else{
            _countryId = provinceId;
            [_mDicID setValue:provinceId forKey:@"县/区"];
        }
    }];
   
}
- (void)_superiorAgenBtnClike:(UIButton *)btn{
    
    if ([_provinceSetBtn.titleLabel.text isEqualToString:@"点击设置"]) {
        [self showMsg:@"请先选择您的省位置"];
        return;
    }else if ([_citySetBtn.titleLabel.text isEqualToString:@"点击设置"]){
        [self showMsg:@"请先选择您的市位置"];
        return;
    }else{
   
    ExamineController *examin = [[ExamineController alloc] init];
    examin.mDicID = _mDicID;
      
    if (!_mDicID[@"县/区"]) {
        examin.typeStr = @"2";
    }else{
        examin.typeStr = @"3";
    }

    [examin shopIdStr:^(NSString *str, NSString *strId) {
        _shopManastr = str;
//          NSLog(@"_shopManastr == %@",str);
        NSArray *compa = [str componentsSeparatedByString:@" "];
        _pID = strId;
        [btn setTitle:compa[0] forState:UIControlStateNormal];
    }];
    
    [self.navigationController pushViewController:examin animated:YES];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
